﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using BaseData.Common;
using BaseData.UI;
using TaxData.SysConfig;
using TaxEntity.SysConfig;
using TaxEntity.User;

namespace TaxBiz.SysConfig
{
    // <summary>
    /// 税种逻辑层
    /// </summary>
    public class TaxsBiz
    {
        private TaxsData td = new TaxsData();
        
        /// <summary>
        /// 获取税种信息实体
        /// </summary>
        public TaxsEntity GetEntityById(int ID)
        {
            TaxsEntity te = td.GetEntityById(ID);
            return te;
        }

        /// <summary>
        /// 获取税种列表
        /// </summary>
        public DataTable GetList()
        {
            string cacheName = "TaxsBiz_GetList_All";
            DataTable dt = Cache.GetData(cacheName) as DataTable;
            if (dt == null)
            {
                dt = td.GetList();
                Cache.Add(cacheName, dt, DateTime.Now.AddHours(2));
            }
            return dt;
        }

        /// <summary>
        /// 获取应税税种列表
        /// </summary>
        public Dictionary<string, string> GetRequireList()
        {
            DataTable dt = this.GetList();
            Dictionary<string, string> d = new Dictionary<string, string>();
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow[] drs = dt.Select("IsShow=1");
                if (drs != null && drs.Length > 0)
                {
                    for (int i = 0; i < drs.Length; i++)
                    {
                        if(!d.ContainsKey(drs[i]["DbName"].ToString().ToLower()))
                        {
                            d.Add(drs[i]["DbName"].ToString().ToLower(),drs[i]["Name"].ToString());
                        }
                    }
                }
            }
            return d;
        }

        /// <summary>
        /// 获取已税、应税记录
        /// </summary>
        public void GetTaxDescDic(DataRow dr, out Dictionary<string,decimal> requireTaxDic, out Dictionary<string,decimal> completeTaxDic)
        {
            requireTaxDic = new Dictionary<string,decimal>();
            completeTaxDic = new Dictionary<string,decimal>();
            if (dr == null)
            {
                return;
            }
            Dictionary<string, string> taxDic = GetRequireList();
            foreach (DataColumn c in dr.Table.Columns)
            {
                string key = c.ColumnName.ToLower();
                //应税
                if (taxDic.ContainsKey(key))
                {
                    decimal d = Kernel.GetDecimalFromObject(dr[c]);
                    if (d > 0)
                    {
                        requireTaxDic.Add(taxDic[key], d);
                    }
                }
                //已税
                key = c.ColumnName.ToLower().Substring(1);
                if (taxDic.ContainsKey(key))
                {
                    decimal d = Kernel.GetDecimalFromObject(dr[c]);
                    if (d > 0)
                    {
                        completeTaxDic.Add(taxDic[key], d);
                    }
                }
            }
        }

        /// <summary>
        /// 获取已税、应税、补税记录
        /// </summary>
        public Dictionary<string, decimal> GetBujiaoTaxDic(DataRow dr)
        {
            Dictionary<string, decimal> requireTaxDic = new Dictionary<string, decimal>();
            Dictionary<string, decimal> completeTaxDic = new Dictionary<string, decimal>();
            GetTaxDescDic(dr, out requireTaxDic, out completeTaxDic);
            Dictionary<string, decimal> bujiaoTaxDic = new Dictionary<string, decimal>();
            foreach (KeyValuePair<string, decimal> pair in requireTaxDic)
            {
                bool isNeedBujiao = true;
                decimal bujiao = 0;
                if (completeTaxDic.ContainsKey(pair.Key))
                {
                    if (pair.Value <= completeTaxDic[pair.Key])
                    {
                        isNeedBujiao = false;
                    }
                    else
                    {
                        bujiao = pair.Value - completeTaxDic[pair.Key];
                    }
                }
                else
                {
                    bujiao = pair.Value;
                }
                if (isNeedBujiao)
                {
                    bujiaoTaxDic.Add(pair.Key, bujiao);
                }
            }
            return bujiaoTaxDic;
        }

        /// <summary>
        /// 删除税种
        /// </summary>
        public bool Delete(int ID, UserEntity optUser)
        {
            bool isSucc = td.Delete(ID);
            if (isSucc)
            {
                this.ClearCache();
            }
            return isSucc;
        }

        /// <summary>
        /// 修改税种信息
        /// </summary>
        /// <param name="model">修改税种</param>
        /// <param name="optUser">操作人</param>
        /// <returns>操作结果</returns>
        public bool Update(TaxsEntity model, UserEntity optUser)
        {
            bool isSucc = td.Update(model);
            if (isSucc)
            {
                this.ClearCache();
            }
            return isSucc;
        }

        /// <summary>
        /// 清除缓存
        /// </summary>
        private void ClearCache()
        {
            string cacheName = "TaxsBiz_GetList_All";
            Cache.Remove(cacheName);
        }
    }
}
